package com.lanchetech.user;

import com.lanchetech.bean.vo.CardConsumptionUserVo;
import com.lanchetech.dao.CardConsumptionUserMapper;
import com.lanchetech.entity.CardConsumptionUser;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.*;

import static com.google.common.math.IntMath.factorial;

public class AsyncThread extends Thread {


    @Autowired
    private CardConsumptionUserMapper cardConsumptionUserMapper;

    @Override
    public void run() {
        System.out.println("当前线程名称" + this.getName() + ",执行线程名称" + Thread.currentThread().getName() + "-Hello");
    }

    public static void main(String[] args) {
        // 创建一个CompletableFuture   CompletableFuture.supplyAsync异步的 他的参数是一个 Supplier函数式借口 提供一个子任务
        CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
                System.out.println(1 * 1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "执行完成 lamdba 执行完成";
        });

        // 子任务执行成功 回调给方法 参数是一个 Consumer的 函数式接口
        cf.thenAccept(result -> {
            System.out.println("子任务执行成功");
            System.out.println("result = " + result);
        });

        // 子任务执行成功失败 回调这个方法  参数是一个 Function<Throwable, ? extends T>函数式借口
        cf.exceptionally(e -> {
            System.out.println("e = " + e.getMessage());
            return "子任务执行成功失败";
        });
        cf.join();
    }


}
